package com.lh.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.api.dao.ActivityShopOneMapper;
import com.lh.api.dao.DoboCodeMapper;
import com.lh.api.dao.PeriodMapper;
import com.lh.api.service.ActivityShopOneService;
import com.lh.api.service.DoboCodeService;
import com.lh.api.vo.DoboCodeVo;
import com.lh.api.vo.DoboDetailVo;
import com.lh.api.vo.PropertyVo;
import com.lh.api.vo.ShopPeriodVo;
import com.lh.common.entity.admin.ActivityShopOne;
import com.lh.common.entity.admin.Period;
import com.lh.common.enums.PeriodStatusEnum;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.HideDataUtil;
import com.lh.common.utils.PageUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Gze
 * @ClassName: ShopServiceImpl
 * @Description: (类作用)
 * @date: 2020-05-15 15:35
 */
@Service
public class ActivityShopOneServiceImpl extends ServiceImpl<ActivityShopOneMapper, ActivityShopOne> implements ActivityShopOneService {

    @Autowired
    private PeriodMapper periodMapper;

    @Autowired
    private DoboCodeMapper doboCodeMapper;

    @Autowired
    private DoboCodeService doboCodeService;

    @Override
    public DoboDetailVo getDoboShopDetail(Long aid, Long pid) {
        if (aid == null){
            return new DoboDetailVo();
        }

        //查询活动及商品详情详情
        DoboDetailVo result = baseMapper.getDoboShopDetail(aid);
        if (result == null){
            return new DoboDetailVo();
        }
        result.setAid(aid);

        //整理数据
        result.setSystemTime(System.currentTimeMillis());
        result.setTimeZoneId(TimeZone.getDefault().getID());

        if(StringUtils.isNotEmpty(result.getProperty())){
            result.setPropertyList(parseData(result.getProperty()));
        }

        result.setPercent(new BigDecimal(0));
        if(result.getPortionNum() > 0 ){
            result.setPercent(new BigDecimal(result.getCurrenPortion() * 100).divide(new BigDecimal(result.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
        }

        if(StringUtils.isNotEmpty(result.getImg())){
            result.setImgList(Arrays.asList(result.getImg().split(",")));
        }

        //查询期数
        List<ShopPeriodVo> shopPeriodVos = new ArrayList<>();
        if(pid !=null && !pid.equals(result.getPid())){
            DoboDetailVo doboDetailVo = periodMapper.getPeriodHisDetail(aid,pid);
            AssertUtil.assertNotNull(doboDetailVo, Constant.RESULT.FI1000, "Wrong NO.No corresponding activity found");
            result.setUserName(doboDetailVo.getUserName());
            result.setPhone(HideDataUtil.hidePhoneNo(doboDetailVo.getPhone()));
            result.setCode(doboDetailVo.getCode());

            ShopPeriodVo temp =new ShopPeriodVo();
            temp.setPeriod(result.getPeriod());
            temp.setPid(result.getPid());
            temp.setStatus(result.getStatus());
            shopPeriodVos.add(temp);
            shopPeriodVos.addAll(periodMapper.getPeriodHis(aid,pid));
            //设置往期状态
            result.setStatus(PeriodStatusEnum.REVEALED);
            result.setPid(pid);
        }else {
            shopPeriodVos =periodMapper.getPeriodlist(aid);
        }

        result.setPeriodList(shopPeriodVos);
        return result;
    }

    private List<PropertyVo> parseData(String property) {
        JSONArray jsonArray = JSONArray.fromObject(property);
        List<PropertyVo> propertyVoList = new ArrayList<>();
        PropertyVo temp = null;
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            temp = new PropertyVo();
            temp.setTitle(jsonObject.get("title") == null ? "" : jsonObject.get("title").toString());
            temp.setValue(jsonObject.get("value") == null ? "" : jsonObject.get("value").toString());
            propertyVoList.add(temp);
        }
        return propertyVoList;
    }

    @Override
    public List<DoboCodeVo> getDoboCode(Long pid) {
        List<DoboCodeVo> tempList = doboCodeMapper.getDoboCode30(pid);
        List<DoboCodeVo> result = new ArrayList<>();
        DoboCodeVo temp = null;
        for (DoboCodeVo doboCodeVo : tempList){
            if(StringUtils.isNotEmpty(doboCodeVo.getPeriodCode())){
                String[] codeArray = doboCodeVo.getPeriodCode().split(",");
                for (int i = 0; i < codeArray.length; i++){
                    //只返回三十个夺宝码
                    if(i > 29){
                        break;
                    }
                    temp = new DoboCodeVo();
                    temp.setCode(codeArray[i]);
                    temp.setName(doboCodeVo.getName());
                    temp.setPhone(HideDataUtil.hidePhoneNo(doboCodeVo.getPhone()));
                    result.add(temp);
                }
            }
        }
        //补充购买的夺宝码记录
        if(CollectionUtils.isEmpty(result) || result.size() < 30){
            Period period = periodMapper.selectById(pid);
            result = doboCodeService.getDoboCodeList(period, result);
        }
        return result;
    }

    @Override
    public PageUtils<ShopPeriodVo> getPeriodList(Map<String, Object> param, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<ShopPeriodVo> afterSaleListVos = this.periodMapper.getListByMap(param);
        return new PageUtils<>(afterSaleListVos);
    }
}

